<?
	chdir("..");
	include("common.php");
	include("siteadmin/accesscontrol.php");
	include("siteadmin/header.php");
	$att_path = "banners/";
?>
<?
	$n1 = $n2 = 0;
	if ( preg_match("/delete/i", $_POST['change3']) ){
		$qr1 = mysql_query("SELECT * FROM banners");
		$max = 0;
		while ($d = mysql_fetch_object($qr1)){
			$key = $_POST["delete{$d->id}"];
			if($key){
				if($d->id == $key){
					$id=$d->id;
					@mysql_query("DELETE FROM banners WHERE id=$id");
					echo "{$d->html} was deleted<bR>";
					$uC++;
				}
			}
		}
	}
	if ($_POST['change3']){
		$qr1 = mysql_query("SELECT * FROM banners");
		$max = 0;
		while ($a = mysql_fetch_object($qr1)){
			$x[$a->id] = 1;
			if ($a->id > $max) $max = $a->id;
			if (preg_match("/update/i", $_POST['change3'])){
				$query = "UPDATE banners SET html='".addslashes($_POST["html$a->id"])."' WHERE id=$a->id";
				mysql_query($query) or die( mysql_error()."<br>$query<br>" );
			}
		}
		for ($i = 0; $i <= $max; $i++){
			if (!$x[$i]) break;
			$sel_id = $i;
		}
	}
	if( ($_POST['advertise']) ){
		$html = $_POST['html'];
		if($_POST['edit']){
			$query = "UPDATE banners SET 
							html='$html',
							WHERE id=".$_POST['edit'];
		}else{
			$now = time();
			$query = "INSERT INTO banners SET 
							html='$html',
							date='$now'";
		}
		mysql_query($query);
		$pid = mysql_insert_id();
		$filename = (!$_FILES['imgfile']['error'] ? substr( basename($_FILES['imgfile']['name']), -30 ) : '');
		$x = strtolower(substr($filename, -4));
		if ($x == '.php' || $x == '.cgi'){
			$filename = substr($filename, -26).'.txt';
		}
		if ($filename){
			$newname = $att_path."xban".$pid."_".$filename;
			copy($_FILES['imgfile']['tmp_name'], $newname);
		}
		echo "Your banner has been successfully added<br>";
	}
?>
<!------///////////////--->
<div class="titlebar" style="width:50%;height:20px;padding-left:10px;padding-top:5px;color: white;"><b>
Banner Ads
</b></div>
<!------///////////////--->
<TABLE width=100% cellspacing=0>
<tr>
	<td style="padding-left:10px;">&nbsp;</td>
	<TD>
		<TABLE class=design width=90% cellspacing=0>
		<FORM name=form1 method=post>
		<TR>
			<TH>&nbsp;
			<TH>ID
			<TH>Code
			<TH>Impressions
			<TH>Click-thrus
			<TH>Ratio
<?
		if($b_width){
			$imgstr = " width=$b_width";
		}
		if($b_height){
			$imgstr .= " height=$b_height";
		}
		$qr1 = mysql_query("SELECT * FROM banners ORDER BY id ASC");
		$qr2 = mysql_query("SELECT * FROM banners ORDER BY id ASC");
		$itotal = 0;
		for ($i = mysql_num_rows($qr2) - 1; $i >= 0; $i--){
				$itotal++;
		}
		$i = 1;
		while ($a = mysql_fetch_object($qr1)){
			if($a->numImps > 0){
				$ratio = number_format( (($a->numClicks / $a->numImps) * 100), 2 )." %";
			}else{
				$ratio = "0 %";
			}
?>
			<TR>
				<TD width=15><input type=checkbox name="delete<?=$a->id?>" value="<?=$a->id?>">
				<TD width=15><?=$i?></TD> 
				<TD>
<?
			$handle=opendir($att_path); 
			while (false!==($file = readdir($handle))) { 
				if ($file != "." && $file != "..") { 
					if( strstr($file , "xban".$a->id."_") ){
					$furl = $siteurl.(str_replace("./","",$att_path))."/".$file;
?>
					<img src="<?=$furl?>" border=0 <?=$imgstr?>><br>
<?
					}
				} 
			}
			closedir($handle); 
?>
				<input type=text name=html<?=$a->id?> size=50 value="<?=htmlspecialchars($a->html)?>">
			</TD>
			<TD><?=$a->numImps?>
			<TD><?=$a->numClicks?>
			<TD><?=$ratio?>		
<?
	}
?>
			<input type="hidden" name="itotal" value="<?=$itotal?>">
		<TR><TH colspan=14><P align=center><input type=submit name=change3 value="Update">
			<input type=submit name=change3 value="Delete selected" <?=$del_confirm?>></TH></TR>
		</FORM>
		</TABLE>
		<FORM method=post enctype='multipart/form-data'>
<?		if($_REQUEST['edit']){	?>
			<input type="hidden" name="edit" value="<?=$_REQUEST['edit']?>">
<?		}	?>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class=design>
		<tr>
			<th colspan=2>Advertise</th>
		</tr>
		<tr> 
			<th align="center">
				<font size="2" face="Arial, Helvetica, sans-serif">Banner Image<br>
				<span class=tiny>
<?		if($b_width && $b_height){
			echo $b_width." x ".$b_height;
		}
?>
				</span>
			</th>
			<td><input name="imgfile" type="file" size="45"></td>
		</tr>
		<tr> 
			<th align="center"><font size="2" face="Arial, Helvetica, sans-serif">Banner URL</th>
			<td><input name="html" size="45" value="<?=$_POST['html']?>"></td>
		</tr>
		<TR><TH class=submit colspan=2><input type=submit name=advertise value='Advertise >>'></TH></TR>
		</table>
		</form>

	</td>
</tr>
</table>
</CENTER>
<?
	include("siteadmin/footer.php");
?>